define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wdsxh/activity/index' + location.search,
                    add_url: 'wdsxh/activity/add',
                    edit_url: 'wdsxh/activity/edit',
                    del_url: 'wdsxh/activity/del',
                    multi_url: 'wdsxh/activity/multi',
                    import_url: 'wdsxh/activity/import',
                    table: 'wdsxh_activity',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'cat.name', title: __('Unit_cat')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'time', title: __('Time'), operate: 'LIKE'},
                        {field: 'issuer', title: __('Issuer'), operate: 'LIKE'},
                        // {field: 'price', title: __('Price'), operate:'BETWEEN',formatter:function (val,row) {
                        //         var price=row.price;
                        //         var vip_price=row.vip_price;
                        //         var str='';
                        //         if(vip_price==0){
                        //             str+='会员免费'+'</br>';
                        //         }else{
                        //             str+='会员：¥'+vip_price+'</br>';
                        //         }
                        //         if(price==0){
                        //             str+='非会员免费';
                        //         }else{
                        //             str+='非会员：¥'+price;
                        //         }
                        //         return str;
                        //     }},
                        {field: 'population', title: __('活动人数')},
                        {field: 'applytotal', title: __('报名人数')},
                        {field: 'images', title: __('Images'), events: Table.api.events.image,formatter: Table.api.formatter.images},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'linkman', title: __('Linkman'), operate: 'LIKE'},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'show', title: __('Show'), searchList: {"all":__('Show all'),"member":__('Show member')}, formatter: Table.api.formatter.status},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons:[
                                {
                                    name: 'list',
                                    text: __('报名列表'),
                                    title: __('报名列表'),
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'wdsxh/activity/lists?activity_id={id}',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            var datetime=new Date();
            var enddate=new Date();
            enddate=enddate.setDate(enddate.getDate()+1);
            enddate=new Date(enddate);
            require(['bootstrap-daterangepicker'], function(){
                $("#c-time").daterangepicker({
                    startDate:datetime.toLocaleDateString(),
                    endDate:enddate.toLocaleDateString(),
                    timePicker: true,
                    timePicker24Hour:true,
                    locale: {
                        format: 'YYYY/MM/DD HH:mm',
                        applyLabel: '确认',
                        cancelLabel: '取消',
                    },
                    function(start, end, label) {
                        console.log('New date range selected: ' + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD') + ' (predefined range: ' + label + ')');
                    }
                });
            });

            $("[data-toggle='addresspicker']").data("callback", function(res){
                console.log(res);
            });
        },
        edit: function () {
            Controller.api.bindevent();
            require(['bootstrap-daterangepicker'], function(){
                $("#c-time").daterangepicker({
                    timePicker: true,
                    timePicker24Hour:true,
                    locale: {
                        format: 'YYYY/MM/DD HH:mm',
                        applyLabel: '确认',
                        cancelLabel: '取消',
                    },
                    function(start, end, label) {
                        console.log('New date range selected: ' + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD') + ' (predefined range: ' + label + ')');
                    }
                });
            });
        },
        lists:function(){
            Table.api.init({
                extend: {
                }
            });
            console.log(Config.activity);
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: 'wdsxh/activity/lists'+ location.search,
                pk: 'id',
                sortName: 'weigh',
                search:false,
                showToggle: false,
                showColumns: false,
                visible: false,
                showExport: false,
                columns: [
                    [
                        {field: 'id', title: __('ID')},
                        {field: 'user.avatar', title: __('头像'),operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'user.nickname', title: __('昵称'),operate: false},
                        {field: 'member.name', title: __('会员姓名'),operate: false},
                        {field: 'member.mobile', title: __('会员电话'),operate: false},
                        {field: 'activity.title', title: __('活动标题'),operate: false},
                        {field: 'activity.time', title: __('活动时间'),operate: false},
                        // {field: 'money', title: __('报名费用'), operate:'BETWEEN'},
                        // {field: 'status', title: __('支付状态'), searchList: {"0":__('未支付'),"1":__('已支付'),"2":__('退款中'),"3":__('已退款'),"4":__('已完成')}, formatter: Table.api.formatter.status},
                        {field: 'status', title: __('报名状态'), searchList: {"0":__('待审核'),"1":__('已报名'),"3":__('已驳回'),"4":__('已结束')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('操作'),
                            table: table,
                            visible:Config.activity.pay_type == 1?false:true,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'agree',
                                    // text: __('线下已付款'),
                                    text: __('通过'),
                                    // title: __('线下已付款'),
                                    title: __('通过'),
                                    classname: 'btn btn-xs btn-info btn-magic btn-ajax',
                                    // icon: 'fa fa-yen',
                                    url: 'wdsxh/activity/payment',
                                    // confirm: '确认已线下付款完成？',
                                    confirm: '确认审核通过？',
                                    visible:function (row){
                                        if(row.status == 0){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    success: function (data, ret) {
                                        $("#table").bootstrapTable('refresh',{});
                                    },
                                    error: function (data, ret) {
                                        Toastr.error(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'refund',
                                    // text: __('退款'),
                                    text: __('拒绝'),
                                    // title: __('退款'),
                                    title: __('拒绝'),
                                    classname: 'btn btn-xs btn-info btn-magic btn-ajax',
                                    // icon: 'fa fa-yen',
                                    url: 'wdsxh/activity/refund',
                                    // confirm: '确认已线下退款完成？',
                                    confirm: '确认拒绝？',
                                    visible:function (row){
                                        // if(row.status == 2){
                                        //     return true;
                                        // }else{
                                        //     return false;
                                        // }
                                        if(row.status == 0){
                                            return true;
                                        }
                                    },
                                    success: function (data, ret) {
                                        $("#table").bootstrapTable('refresh',{});
                                    },
                                    error: function (data, ret) {
                                        Toastr.error(ret.msg);
                                        return false;
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        }

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on('click','.btn-refresh',function () {
                $("#table").bootstrapTable('refresh',{});
            });
        },
        api: {
            bindevent: function () {
                // $(document).on("change","input[name='row[type]']",function (e){
                //     var val=$("input[name='row[type]']:checked").val();
                //     if(val == 1){
                //         $("[data-input-id='c-address']").attr("disabled","disabled");
                //     }else{
                //         $("[data-input-id='c-address']").removeAttr("disabled");
                //     }
                // });

                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});